package com.appkubes.mall.restful.application.promoter.controller;

import com.appkubes.common.Response;
import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.mall.core.controller.BaseMallPromoterController;
import com.appkubes.mall.core.entity.MallPromoter;
import com.appkubes.mall.core.entity.MallUser;
import com.appkubes.mall.core.service.BaseMallPromoterService;
import com.appkubes.mall.core.service.BaseMallUserService;
import com.appkubes.mall.restful.application.promoter.entity.eo.PromoterDownloadVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author: Rongjie
 * @Date: 2020/2/27
 * @Description: 推广员模块
 */
@Api(tags = "推广员模块(PromoterController)")
@RestController
@RequestMapping("/promoter")
public class PromoterController extends BaseMallPromoterController {

    @Resource
    BaseMallUserService mallUserService;
    @Resource
    BaseMallPromoterService mallPromoterService;


    @ApiOperation("下载量")
    @GetMapping("/downloadStatis")
    public Response downloadStatis(PromoterDownloadVO promoterDownloadVO) {
        MallUser entity = new MallUser();
        entity.setTel(promoterDownloadVO.getTel());
        MallUser mallUser = mallUserService.getOneByEntity(entity);
        if (mallUser == null) return Response.error("用户不存在");
        if (mallUser.getIsDownload() != 1) {
            MallPromoter mallPromoter = mallPromoterService.getById(promoterDownloadVO.getPromoterKey());
            if (mallPromoter != null) {
                mallPromoter.setStatisDownloadCount(mallPromoter.getStatisDownloadCount() + 1);
                mallPromoterService.updateById(mallPromoter);
                mallUser.setIsDownload(1);
                mallUserService.updateById(mallUser);
                return Response.success();
            } else {
                return Response.error("找不到推广员");
            }
        }  else {
            return Response.error("用户已下载过");
        }
    }
}
